package com.example.aircraftticketingsystemserver.service.impl;

import com.example.aircraftticketingsystemserver.entity.Flight;
import com.example.aircraftticketingsystemserver.entity.Ticket;
import com.example.aircraftticketingsystemserver.entity.TransferFlight;
import com.example.aircraftticketingsystemserver.mapper.FlightsMapper;
import com.example.aircraftticketingsystemserver.service.IFlightsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.aircraftticketingsystemserver.utils.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lxz
 * @since 2024-08-05
 */
@Service
@Transactional
public class FlightsServiceImpl extends ServiceImpl<FlightsMapper, Flight> implements IFlightsService {
    @Resource
    private FlightsMapper flightsMapper;
    @Override
    public void addFlight(Flight flight) {
        flightsMapper.addFlight((flight));
    }

    @Override
    public void addTicketByFlightId(Flight flight,Ticket ticket) {
        flightsMapper.addFlight(flight);
        Integer flightId=flight.getFlightId();
        ticket.setFlightId(flightId);
        flightsMapper.addTicketByFlightId(ticket);
    }

    @Override
    public void deleteFlightByFlightId(Integer flightId) {
        flightsMapper.deleteFlightByFlightId((flightId));
    }

    @Override
    public void batchDeleteFlight(List<Integer> flightsId) {
        flightsMapper.batchDeleteFlight(flightsId);
    }

    @Override
    public void updateFlight(Flight flight) {
        flightsMapper.updateFlight(flight);
    }

    @Override
    public Page getFlightsPage(Integer pageNo, Integer pageSize, String flightNumber, String airline, String departureCity, String arrivalCity, String departureAirport, String arrivalAirport,String status, Date start, Date end) {
        Page page=new Page();
        PageHelper.startPage(pageNo,pageSize);
        List<Flight> flights = flightsMapper.getFlightsByParams(flightNumber, airline, departureCity, arrivalCity, departureAirport, arrivalAirport, status,start, end);
        PageInfo<Flight> info = new PageInfo<>(flights);
        page.setPage(info.getList());
        page.setPageNo(info.getPageNum());
        page.setPageSize(info.getPageSize());
        page.setHasNext(info.isHasNextPage());
        page.setHasPre(info.isHasPreviousPage());
        page.setTotal(info.getTotal());
        page.setPageCount(info.getPages());
        return page;
    }

    @Override
    public List<Flight> getFLightList(String flightNumber, String airline, String departureCity, String arrivalCity, String departureAirport, String arrivalAirport, String status, Date start, Date end){
        return flightsMapper.getFlightsByParams(flightNumber, airline, departureCity, arrivalCity, departureAirport, arrivalAirport, status, start, end);
    }

    @Override
    public List<TransferFlight> getTransferFlight(String departureCity, String arrivalCity) {
        return flightsMapper.findTransferFlights(departureCity, arrivalCity);
    }

    @Override
    public Flight getFlightById(Integer flightId) {
        return flightsMapper.selectById(flightId);
    }
}
